package br.com.felix.easyclearcase.ui.dialogs;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 * Dialog to get the user input for the ListCreatedAfterDate command.
 * Makes validation.
 * */
public class ListCreatedAfterDateDialog extends JDialog {
	
	private static final long serialVersionUID = 6900132158235419732L;
	private JPanel panelPrincipal;
	private JButton btnCommit, btnSkip;
	private JTextField txtData;
	private SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
	
	public ListCreatedAfterDateDialog(JFrame parent){
		super(parent);
		panelPrincipal = new JPanel();
		Dimension dimension = new Dimension(250,150);
		this.setPreferredSize(dimension);
		this.setSize(dimension);
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		this.add(panelPrincipal);
		
		panelPrincipal.setLayout(new GridBagLayout());
		JLabel lblArquivo = new JLabel("Date:");
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.anchor = GridBagConstraints.NORTHWEST;
		gbc.insets = new Insets(0,10,10,10);
		gbc.gridx = 0;
		gbc.gridy = 0;
		panelPrincipal.add(lblArquivo,gbc);
		
		
		txtData = new JTextField();
		txtData.setColumns(15);
		txtData.addKeyListener(new KeyAdapter(){
			@Override
			public void keyReleased(KeyEvent e) {
				Pattern pattern = Pattern.compile("\\d{2}/\\d{2}/\\d{4}");
				String data = txtData.getText();
				Matcher m = pattern.matcher(data);
				if(m.matches()){
					try {
						sdf.parse(data);
						btnCommit.setEnabled(true);
					}
					catch (ParseException e1) {
						btnCommit.setEnabled(false);
					}
				}
				else{
					btnCommit.setEnabled(false);
				}
			}
		});
		gbc.gridx += 1;
		panelPrincipal.add(txtData,gbc);
		
		btnCommit = new JButton("Ok");
		btnCommit.setEnabled(false);
		btnSkip = new JButton("Cancel");
		
		JPanel panelBotoes = new JPanel();
		panelBotoes.setPreferredSize(new Dimension(200,50));
		panelBotoes.setLayout(new GridBagLayout());
		gbc = new GridBagConstraints();
		gbc.anchor = GridBagConstraints.NORTHWEST;
		gbc.gridwidth=1;
		gbc.gridy=0;
		gbc.gridx=0;
		panelBotoes.add(btnCommit,gbc);
		gbc.gridx=1;
		panelBotoes.add(btnSkip,gbc);
		
		
		gbc.gridx = 0;
		gbc.gridy = 1;
		gbc.gridwidth = 2;
		panelPrincipal.add(panelBotoes,gbc);

		MouseAdapter disposeMouseAdapter = new MouseAdapter(){
					@Override
					public void mouseReleased(MouseEvent e) {
						dispose();
					}
				};
				
		btnCommit.addMouseListener(disposeMouseAdapter);
		btnSkip.addMouseListener(disposeMouseAdapter);
	}
	
	
	public static void main(String args[]){
		JFrame jfr = new JFrame();
		jfr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		ListCreatedAfterDateDialog cd = new ListCreatedAfterDateDialog(jfr);
		cd.setModal(true);
		jfr.setSize(new Dimension(1024,768));
		jfr.setVisible(true);
		cd.setVisible(true);
	}
	
	public void addCommitMouseListener(MouseListener ml){
		btnCommit.addMouseListener(ml);
	}
	
	public void addCancelMouseListener(MouseListener ml){
		btnSkip.addMouseListener(ml);
	}
	
	
	/**
	 * Returns the date the user has inputted.
	 * */
	public Date getDate(){
		try {
			return sdf.parse(txtData.getText());
		}
		catch (ParseException e) {
			return null;
		}
	}
}
